package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;
import java.util.Random;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.Game1Activity;
import flc.ast.activity.Game2Activity;
import flc.ast.adapter.CommunityRecAdapter;
import flc.ast.adapter.TopicAdapter;
import flc.ast.databinding.FragmentCommunityBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CommunityFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CommunityFragment extends BaseNoModelFragment<FragmentCommunityBinding> {

    private TopicAdapter topicAdapter;
    private CommunityRecAdapter recAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.fragment_community;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        
        mDataBinding.ivGame1.setOnClickListener(this);
        mDataBinding.ivGame2.setOnClickListener(this);
        mDataBinding.ivTop.setOnClickListener(this);


        mDataBinding.rvTopicList.setLayoutManager(new LinearLayoutManager(mContext));
        topicAdapter = new TopicAdapter();
        mDataBinding.rvTopicList.setAdapter(topicAdapter);
        topicAdapter.setOnItemClickListener(this);

        mDataBinding.rvRecommendList.setLayoutManager(new LinearLayoutManager(mContext));
        recAdapter = new CommunityRecAdapter();
        mDataBinding.rvRecommendList.setAdapter(recAdapter);
        recAdapter.setOnItemClickListener(this);

        mDataBinding.refreshRecommendItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshRecommendItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshRecommendItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getRecData();
                mDataBinding.refreshRecommendItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getRecData();
                mDataBinding.refreshRecommendItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivGame1://看图猜电视剧
                startActivity(Game1Activity.class);
                break;
            case R.id.ivGame2://看图猜角色
                startActivity(Game2Activity.class);
                break;
            case R.id.ivTop://置顶
                mDataBinding.scrollView.fullScroll(View.FOCUS_UP);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        Random random = new Random();
        mDataBinding.tvGame1Num.setText(random.nextInt(5000) + "");
        mDataBinding.tvGame2Num.setText(random.nextInt(5000) + "");

        getTopicData();
        getRecData();
    }

    private void getTopicData() {
        StkResApi.getTagResourceList(this, AppConfig.TOPIC_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        topicAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getRecData() {
        StkResApi.getTagResourceList(this, AppConfig.COMMUNITY_REC_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            recAdapter.setList(result);
                        } else {
                            recAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

}